package com.example.springbootdemo1.demos.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        User user = new User();
        user.setName("zhangsan");
        user.setAge(18);
        user.setId(12);
        //对象转json
        String s= objectMapper.writeValueAsString(user);
        System.out.println(s);
        //json字符串转java对象
        User user2 = objectMapper.readValue(s, User.class);
        System.out.println(user2);
    }
}
